<html>
<head><meta charset="utf-8"><title>difference between `iter()` and `into_iter()` · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html">difference between `iter()` and `into_iter()`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="213672430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/difference%20between%20%60iter%28%29%60%20and%20%60into_iter%28%29%60/near/213672430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html#213672430">(Oct 17 2020 at 21:52)</a>:</h4>
<p>Is there difference between <code>Vec::iter()</code> and <code>&lt;Vec as IntoIterator&gt;::into_iter()</code> other than <code>Vec::iter()</code> taking <code>&amp;self</code> and <code>into_iter()</code> taking owned <code>self</code>?</p>



<a name="213672436"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/difference%20between%20%60iter%28%29%60%20and%20%60into_iter%28%29%60/near/213672436" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html#213672436">(Oct 17 2020 at 21:52)</a>:</h4>
<p>I couldn't find any documentation on when you should use one over the other.</p>



<a name="213675260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/difference%20between%20%60iter%28%29%60%20and%20%60into_iter%28%29%60/near/213675260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html#213675260">(Oct 17 2020 at 23:10)</a>:</h4>
<p>TL;DR: No, there isn't really much more to it. </p>
<ul>
<li><code>Vec.iter()</code> == "iterate over all elements in that vector"</li>
<li><code>Vec.into_iter()</code> == "turn the vector into an iterator which yields all values which had been in the vector"</li>
<li><code>iter</code> does not consume the vector and returns references to the values (iter_mut() returns mutable references).</li>
<li><code>into_iter</code> moves the vector into the iterator consuming it and returns owned values (like you mentioned)</li>
<li><code>iter</code>/<code>iter_mut</code> are implemented on slice and accesses through auto-dereferencing the vec to a slice</li>
<li><code>into_iter</code> is a generic trait implemented for <code>Vec</code>. You can be generic over anything implementing <code>IntoIterator</code> but not  over anything having a iter/iter_mut method. Through <code>IntoIterator</code> is implemented for references  (e.g. <code>impl&lt;'a, T&gt; IntoIterator for &amp;'a Vec&lt;T&gt;</code> )acting like the <code>.iter()</code> function.</li>
</ul>



<a name="213675659"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/difference%20between%20%60iter%28%29%60%20and%20%60into_iter%28%29%60/near/213675659" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html#213675659">(Oct 17 2020 at 23:21)</a>:</h4>
<p>So if you still need the <code>Vec</code> after iteration use <code>.iter()</code>/<code>.iter_mut()</code>.</p>
<p>If not and especially if you might move the elements from the vector somewhere else <code>.into_iter()</code> is likely a good idea.</p>



<a name="213676062"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/difference%20between%20%60iter%28%29%60%20and%20%60into_iter%28%29%60/near/213676062" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html#213676062">(Oct 17 2020 at 23:30)</a>:</h4>
<p>Is it bad for performance to use <code>iter()</code> when you could use <code>into_iter()</code>? (I.e., when you don't need to use the vector later, but you also don't need owned values.)</p>



<a name="213676077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/difference%20between%20%60iter%28%29%60%20and%20%60into_iter%28%29%60/near/213676077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html#213676077">(Oct 17 2020 at 23:31)</a>:</h4>
<p>The main reason I ask for that is because <code>iter()</code> is shorter <span aria-label="smile" class="emoji emoji-1f642" role="img" title="smile">:smile:</span></p>



<a name="213676279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/difference%20between%20%60iter%28%29%60%20and%20%60into_iter%28%29%60/near/213676279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html#213676279">(Oct 17 2020 at 23:35)</a>:</h4>
<p>Normally not.</p>
<p>The only difference would be that the vector is dropped later then it would be else wise. <br>
This can matter in some case but in many it doesn't.</p>



<a name="213676821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/difference%20between%20%60iter%28%29%60%20and%20%60into_iter%28%29%60/near/213676821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html#213676821">(Oct 17 2020 at 23:46)</a>:</h4>
<p>But thinks can be somewhat complicated.</p>
<p>Like e.g. if the vector now lives across a <code>.await</code> boundary and didn't before it now needs to be moved into the generator, or if it's big and you spend a lot of time between you no longer using it and it being dropping  it might wast some memory uneccesary. But often the  difference is  small, like if it lives across a <code>.await</code> boundary the size of the generate grows by ~3*usize and if the the vector is not very big the memory allocated longer then necessary might not have been needed by the system eitherway.</p>
<p>In some cases where <code>Drop</code>'ing the elements in the vector is expensive dropping it later can even be good.</p>
<p>But in many use-cases the only relevant drawback is that the memory is freed <em>slightly</em> later, which often doesn't matter either.</p>
<p>So for the beginning I would not worry about it to much. Only if I realize my program is too slow or uses too much memory would I spend time one improving this.  Later when you have a better understanding of how freeing up memory earlier can affect performance and how it can affect generators/futures you might just do thinks more optimal from the get to go. But the main priority is to produce thinks which work correctly, and only if the do optimize for  performance (at least for me).</p>



<a name="213677782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/difference%20between%20%60iter%28%29%60%20and%20%60into_iter%28%29%60/near/213677782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html#213677782">(Oct 18 2020 at 00:08)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> I'd say that if you don't need ownership, then <code>.iter()</code> helps communicate that so it's perfectly reasonable.</p>



<a name="217327303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/difference%20between%20%60iter%28%29%60%20and%20%60into_iter%28%29%60/near/217327303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/difference.20between.20.60iter().60.20and.20.60into_iter().60.html#217327303">(Nov 19 2020 at 20:39)</a>:</h4>
<p>For some uses into_iter can reuse the allocation and thus be more efficient than iter()</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>